import time
from queue import Queue
import threadpool
import threading
import random

consumer_pool = threadpool.ThreadPool(2)
constructor_pool = threadpool.ThreadPool(2)
queue = Queue()
lock = threading.Lock()
lock.acquire()


def generate_number(q):
    lock.acquire()
    lock.release()
    assert isinstance(q, Queue)
    while True:
        time.sleep(0.1)
        num = random.randint(1, 1000)
        print("Generate new number:" + str(num))
        q.put(num)


def consume_number(q):
    lock.acquire()
    lock.release()
    assert isinstance(q, Queue)
    while True:
        time.sleep(0.3)
        num = q.get()
        print("Consuming number: " + str(num) + " left: " + str(q.qsize()))


for i in range(2):
    request = threadpool.WorkRequest(generate_number, args=(queue,))
    constructor_pool.putRequest(request)

for i in range(2):
    request = threadpool.WorkRequest(consume_number, args=(queue,))
    consumer_pool.putRequest(request)

print("Starting consumers and constructors.")
lock.release()
constructor_pool.wait()
consumer_pool.wait()
